﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Astor.Modules.DataSources.Screens.Details;
using Astor.Modules.DataSources.Services;
using System.Collections;
using Astor.Modules.DataSources.Models;
using System.Collections.Generic;
using Moq;
using System.Linq;
using System.Collections.ObjectModel;
using Microsoft.Practices.Unity;

namespace Astor.Modules.DataSources.Tests.Screens.Details
{
    [TestClass]
    public class DataSourceDetailsViewModelFixture
    {
        private IUnityContainer _container;

        [TestInitialize]
        public void InitializeTest()
        {
            _container = new MockContainer();
        }

        [TestMethod]
        public void InitializeAdd()
        {
            //--given
            string title = "Add";
            var vm = _container.Resolve<DataSourceDetailsViewModel>();

            //--when
            vm.InitializeAdd(title);

            //--then
            Assert.AreEqual(title, vm.Title, "Title not assigned.");
            Assert.IsNotNull(vm.DataSource, "DataSource is not initialized.");
        }

        [TestMethod]
        public void InitializeEdit()
        {
            //--given
            string title = "Edit";
            DataSource ds = new DataSource { Id = Guid.NewGuid(), Name = "Name1", Url = "Url1" };
            DataSourceDetailsViewModel vm = _container.Resolve<DataSourceDetailsViewModel>();
            
            //--when
            vm.InitializeEdit(title, ds);

            //--then
            Assert.AreEqual(title, vm.Title, "Title not assigned");
            Assert.AreEqual(ds, vm.DataSource, "DataSource not assigned");
        }

        [TestMethod]
        public void Validate_NoErrors()
        {
            //--given
            DataSourceDetailsViewModel vm = _container.Resolve<DataSourceDetailsViewModel>();
            
            //--when
            vm.InitializeAdd("Add");
            vm.DataSource.Name = "ValidName";
            vm.DataSource.Url = "ValidUrl";
            vm.Validate();

            //--then
            Assert.IsTrue(vm.CanSubmit, "CanSubmit must be true after DataSource assigned valid data.");
        }

        [TestMethod]
        public void Validate_EmptyData()
        {
            //--given
            var vm = _container.Resolve<DataSourceDetailsViewModel>();
            
            //--when
            vm.InitializeAdd("Add");
            vm.Validate();
            
            //--then
            Assert.IsFalse(vm.CanSubmit, "CanSubmit must be false when DataSource has empty data.");
        }

        [TestMethod]
        public void Validate_ExistingName()
        {
            //--given
            var vm = _container.Resolve<DataSourceDetailsViewModel>();
            string existingName = _container.Resolve<IDataSourcesService>().FindAll().First().Name;

            //--when
            vm.InitializeAdd("Add");
            vm.DataSource.Url = "someUrl";
            vm.DataSource.Name = existingName;
            vm.Validate();

            //--then
            Assert.IsFalse(vm.CanSubmit, "CanSubmit must be false when DataSource has already existing name assigned.");
        }
    }
}